package com.common.UITags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * 输入日期的标签
 * @author zhanglong
 *
 */
public class DateInputTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	private String id;//id
	private String name;//name
	private String label;//标签名
	private String mask;//日期显示的样式
	private String required = "false";//是否必录
	private String defaultvalue;//默认值

	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			StringBuffer sBF = new StringBuffer();
			StringBuffer sBFL = new StringBuffer();
			sBFL.setLength(0);
			if(!(label==null||"".equals(label))){
				sBFL.append("<span style=\"font-size: small;vertical-align: middle;font-weight: bold;color:#535C5E;display:inline-block;width:43%;text-align:right;\">" +label);
				//如果为必录项则加上一个红色的星号
				if(required!=null&&!"".equals(required)&&"true".equals(required)){
					sBFL.append("<font color=\"red\">*</font>");
				}
				sBFL.append(":&nbsp;&nbsp;</span>");
			}
			sBF.setLength(0);
			sBF.append("<input type = \"text\"");
			if(!(id==null ||"".equals(id))){
				sBF.append(" id = \""+id+"\" ");
			}
			if(!(name==null ||"".equals(name))){
				sBF.append(" name = \""+name+"\" ");
			}else{
				sBF.append(" name = \""+id+"\" ");
			}
			if(required!=null&&!"".equals(required)){
				sBF.append(" required=\""+required+"\" ");
			}
			if(!(defaultvalue==null||"".equals(defaultvalue))){
				sBF.append("value=\""+defaultvalue+"\" ");
			}
			sBF.append("onclick=\"WdatePicker({");
			if(!(mask==null||"".equals(mask))){
				sBF.append("dateFmt:'" + mask + "'");
				
			}
			sBF.append("})\"");
			
			sBF.append("style=\"");
			//为了好看设置宽度为表格宽度的55%
			if(this.getParent() instanceof FormTableTag){
				sBF.append(" width: 100%;");
			}
			sBF.append("text-align: center; ");
			sBF.append("\"");
			sBF.append("></input>");
			if(this.getParent() instanceof FormTableTag){
				out.print("<td width=\"33%\">" +sBFL.toString() +"<div style=\"width:55%;display:inline-block;\">" +sBF.toString()+"</div>" + "</td>");
			}else{
				out.print(sBF.toString());
			}
		} catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
	
	
	
	
	//get 和set方法
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getMask() {
		return mask;
	}

	public void setMask(String mask) {
		this.mask = mask;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public String getDefaultvalue() {
		return defaultvalue;
	}

	public void setDefaultvalue(String defaultvalue) {
		this.defaultvalue = defaultvalue;
	}
	
	
}
